package com.shop.manager.controller;

//import com.shop.manager.entity.ItemCat;
//import com.shop.manager.http.HttpResult;
//import com.shop.manager.http.PageResult;
import com.base.common.entity.ItemCat;
import com.base.common.http.HttpResult;
import com.base.common.http.PageResult;
import com.shop.manager.service.ItemCatSerice;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 商品分类增删改查
 */
@RestController
@RequestMapping("/itemCat")
public class ItemCatController {

    /**
     * @Description 注入service对象
     */
    @Autowired
    private ItemCatSerice itemCatSerice;

    /**
     * @Description 添加商品分类
     * @param itemCat
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public HttpResult add(@RequestBody(required = false) @ApiParam(value = "ItemCat 对象") ItemCat itemCat) {
        try {
            itemCatSerice.add(itemCat);
            return HttpResult.ok("添加商品分类成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("添加商品分类失败!");
    }

    /**
     * @Description 批量删除商品分类
     * @param ids 分类id的数组
     * @return
     */
    @RequestMapping("/delete")
    public HttpResult delete(@RequestBody Long[] ids) {
        try {
            itemCatSerice.deleteByIds(ids);
            //删除成功
            return HttpResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //删除异常
            return HttpResult.error();
        }
    }

    /**
     * @Description 修改商品分类
     * @param itemCat 修改的商品分类对象
     * @return
     */
    @RequestMapping("/update")
    public HttpResult update(@RequestBody(required = false) @ApiParam(value = "ItemCat 对象") ItemCat itemCat) {
        try {
            itemCatSerice.update(itemCat);
            return HttpResult.ok("修改商品分类成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("修改商品分类失败!");
    }

    /**
     * @Description 根据ID查询商品分类
     * @param id 分类id
     * @return
     */
    @RequestMapping("/getById")
    public HttpResult getById(@RequestParam("id") Long id) {
        return HttpResult.ok(itemCatSerice.findOne(id));
    }

    /**
     * @Description 条件分页查询
     * @param pageNum 当前页码
     * @param size 当前页条数
     * @param itemCat 查询条件对象
     * @return
     */
    @RequestMapping("/getAll")
    public HttpResult getAll(@RequestParam("pageNum") Integer pageNum, @RequestParam("size") Integer size, @RequestBody(required = false) ItemCat itemCat) {
        try {
            //分页查询
            PageResult result = itemCatSerice.findPage(pageNum, size, itemCat);
            //查询成功
            return HttpResult.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            //查询异常
            return HttpResult.error("查询商品分类异常");
        }
    }

    /**
     * @Description 根据ParentId获取分类
     * @param parentId ParentId
     * @return
     */
    @RequestMapping("/parent")
    public HttpResult getByParentId(@RequestParam("parentId") Long parentId) {
        try {
            List<ItemCat> itemCats = itemCatSerice.getByParentId(parentId);
            return HttpResult.ok(itemCats);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.error("查询失败!");
    }

    /**
     * @Description 分页根据ParentId获取分类
     * @param page 当前页码
     * @param rows 当前页条数
     * @param id ParentId
     * @return
     */
    @RequestMapping("/getByParentIdAndPage")
    public HttpResult getByParentIdAndPage(@RequestParam("page") Integer page, @RequestParam("rows") Integer rows, @RequestParam("id") Long id) {
        return HttpResult.ok(itemCatSerice.getByParentIdAndPage(page, rows, id));
    }

    /**
     * @Description 获取完整商品分类
     * 需求：查询首页左侧菜单
     * 业务流程：
     * 1，先查询顶级菜单
     * 2，把菜单封装到集合中
     * 3，循环顶级菜单，根据顶级菜单id查询子节点
     * 4，把查询出来的集合再次进行封装
     * 5，循环二级菜单，根据二级菜单的id查询三级菜单
     * 6，把三级菜单数据封装到集合
     */
    @RequestMapping("/categoryList")
    public HttpResult categoryList(){
        return itemCatSerice.categoryList();
    }

    /**
     * 查询全部商品分类
     * @return
     */
    @RequestMapping("/all")
    public HttpResult all() {
        return itemCatSerice.findAll();
    }

}
